package com.eduction.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.eduction.modules.system.entity.*;
import com.eduction.vo.ApplyFilePost;
import com.eduction.vo.ApplyFileVo;
import com.eduction.vo.DetailVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eduction.bean.SessionUser;
import com.eduction.common.ReturnCode;
import com.eduction.modules.target.dao.MonitorSeeMapRepository;
import com.eduction.modules.target.entity.Detailed;
import com.eduction.modules.target.entity.IndexTargetEntity;
import com.eduction.modules.target.entity.MonitorSeeMap;
import com.eduction.modules.target.entity.ObservationPoint;
import com.eduction.modules.target.entity.SonTarget;
import com.eduction.resource.BaseController;
import com.eduction.service.ApplyFileService;
import com.eduction.service.DetailedService;
import com.eduction.service.IndexTargetEntityService;
import com.eduction.service.MonitorSeeMapService;
import com.eduction.service.ObservationPointService;
import com.eduction.service.SchoolItemDataService;
import com.eduction.service.SchoolService;
import com.eduction.service.SonTargetService;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {

    private static final Logger log = Logger.getLogger(ItemController.class);

    @Autowired
    private DetailedService detailedService;
    @Autowired
    private IndexTargetEntityService indexTargetEntityService;
    @Autowired
    private ObservationPointService observationPointService;
    @Autowired
    private SonTargetService sonTargetService;
    @Autowired
    private MonitorSeeMapService monitorSeeMapService;
    @Autowired
    private ApplyFileService applyFileService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private SchoolItemDataService schoolItemDataService;
    @Autowired
    MonitorSeeMapRepository monitorSeeMapRepository;

    /***************************************************管理员页面************************************************************************************/

    /**
     * 查询所有一级指标
     *
     * @param request
     * @param
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/queryAllIndexTarget", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> queryAllIndexTarget(HttpServletRequest request) {
        try {
            String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
            if(StringUtils.isEmpty(user_type)){
                return toResponse(null,ReturnCode.FAIL,"暂无权限！");
            }

            List<IndexTargetEntity> list = indexTargetEntityService.queryAllIndexTarget();
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "暂无数据");
            List<IndexTargetEntity> lt = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                List<SonTarget> list1 = sonTargetService.querySonTargetByFid(list.get(i).getP_id());
                IndexTargetEntity it = new IndexTargetEntity();
                it.setP_id(list.get(i).getP_id());
                it.setS_name(list.get(i).getS_name());
                it.setS_create_time(list.get(i).getS_create_time());
                it.setS_status(list.get(i).getS_status());
                it.setS_type(list.get(i).getS_type());
                it.setList(list1);
                lt.add(it);
            }
            return toResponse(lt, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 查询单条一级指标
     */
    @RequestMapping(value = "/queryIndexTargetByPid", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryIndexTargetByPid(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("p_id");
        if (pid == null) {
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        }
        try {
            IndexTargetEntity it = indexTargetEntityService.queryIndexTargetById(pid);
            if (it == null)
                return toResponse(null, ReturnCode.FAIL, "该指标不存在!");
            return toResponse(it, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.SUCCESS, "查询失败!");
        }
    }

    /**
     * 添加一级指标
     */
    @RequestMapping(value = "/saveIndexTarget", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveIndexTarget(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String name = map.get("name");
        if (name == null) {
            return toResponse(null, ReturnCode.FAIL, "name为空!");
        }
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        IndexTargetEntity it = new IndexTargetEntity();
        it.setS_name(name);
        it.setS_create_time(createTime);
        it.setS_type(1);
        it.setS_status(1);
        indexTargetEntityService.saveIndexTargetInfo(it);
        return toResponse(it, ReturnCode.SUCCESS, "保存成功!");

    }

    /**
     * 修改指标详情
     */
    @RequestMapping(value = "/updataIndexTarget", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updataIndexTarget(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("p_id");
        String name = map.get("s_name");
        if (pid == null) {
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        }
        try {
            IndexTargetEntity it = indexTargetEntityService.queryIndexTargetById(pid);
            if (it == null) {
                return toResponse(null, ReturnCode.FAIL, "该指标不存在!");
            }
            if (name != null) {
                it.setS_name(name);
            }
            indexTargetEntityService.saveIndexTargetInfo(it);
            return toResponse(it, ReturnCode.SUCCESS, "删除成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.SUCCESS, "删除失败!");
        }
    }

    /**
     * 删除一级指标
     */
    @RequestMapping(value = "/deleteIndexTarget", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteIndexTarget(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("p_id");
        if (pid == null) {
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        }
        try {
            IndexTargetEntity it = indexTargetEntityService.queryIndexTargetById(pid);
            if (it == null) {
                return toResponse(null, ReturnCode.FAIL, "该指标不存在!");
            }
            it.setS_status(2);
            indexTargetEntityService.saveIndexTargetInfo(it);
            return toResponse(it, ReturnCode.SUCCESS, "删除成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.SUCCESS, "删除失败!");
        }
    }

    /**
     * 查询二级指标
     */
    @RequestMapping(value = "/querySonTargetByFid", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> querySonTargetByFid(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String indexId = map.get("pid");
        if (indexId == null) {
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        }
        try {
            List<SonTarget> list = sonTargetService.querySonTargetByFid(indexId);
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "二级指标不存在!");
            return toResponse(list, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 查询单个二级指标
     */
    @RequestMapping(value = "/querySonTargetByPid", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> querySonTargetByPid(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("p_id");
        if (pid == null) {
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        }
        try {
            SonTarget st = sonTargetService.querySonTargetByPid(pid);
            if (st == null)
                return toResponse(null, ReturnCode.FAIL, "该指标不存在!");
            return toResponse(st, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.SUCCESS, "查询失败!");
        }
    }

    /**
     * 修改二级指标详情
     */
    @RequestMapping(value = "/updataSonTarget", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updataSonTarget(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        String name = map.get("name");
        if (pid == null)
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            SonTarget st = sonTargetService.querySonTargetByPid(pid);
            if (st != null)
                if (name != null)
                    st.setS_name(name);
            sonTargetService.saveSonTargetInfo(st);
            return toResponse(st, ReturnCode.SUCCESS, "修改成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "修改失败!");
        }
    }

    /**
     * 添加二级指标
     */
    @RequestMapping(value = "/saveSonTarget", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveSonTarget(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String name = map.get("name");
        String indexId = map.get("indexId");
        if (name == null) {
            return toResponse(null, ReturnCode.FAIL, "name为空!");
        }
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        SonTarget st = new SonTarget();
        st.setS_name(name);
        st.setP_index_id(indexId);
        st.setS_create_time(createTime);
        st.setS_type("1");
        st.setS_status("1");
        sonTargetService.saveSonTargetInfo(st);
        return toResponse(st, ReturnCode.SUCCESS, "添加成功!");
    }

    /**
     * 删除二级指标
     */
    @RequestMapping(value = "/deleteSonTarget", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteSonTarget(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        if (pid == null)
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            SonTarget st = sonTargetService.querySonTargetByPid(pid);
            if (st != null)
                st.setS_status("2");
            sonTargetService.saveSonTargetInfo(st);
            return toResponse(st, ReturnCode.SUCCESS, "删除成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除失败!");
        }
    }

    /**
     * 查询细则,系统管理下
     */
    @RequestMapping(value = "/queryDetailedInManager", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryDetailedInManager(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String fid = map.get("fid");
        String schoolType = map.get("school_type");
        if ("0".equals(schoolType)) {
            schoolType = "1495071155575000";
        } else if ("1".equals(schoolType)) {
            schoolType = "1495071155575001";
        }
        if (fid == null)
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            List<Detailed> list = detailedService.queryDetailedByFid(fid, schoolType);
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "细则不存在!");
            return toResponse(list, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public ResponseEntity<String> delete(@PathVariable("id") String fileid,HttpServletRequest request) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String value = request.getParameter("value");
        if(value.contains("+")){
            value=value.split("\\+")[0];
        }
//        try {
//            value=URLDecoder.decode(value, "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

        if(null==value) {
    		 return toResponse(null, ReturnCode.FAIL, "参数错误!");
    	}
        try {
    	    SchoolItemData schoolItemData=schoolItemDataService.queryById(fileid);
    	    if(schoolItemData!=null) {
                String[] valueList=schoolItemData.getS_value().split(",");
                String s_value="";
                for(String s:valueList){
                    if(s!=null&&!s.contains(value)&&!"".equals(s)){
                        if("".equals(s_value)){
                            s_value=s;
                        }else{
                            s_value=s_value+","+s;
                        }

                    }
                }

                monitorSeeMapRepository.deleteItem(fileid, s_value);
            }
            return toResponse(null, ReturnCode.SUCCESS, "删除成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除失败!");
        }
    }


    /**
     * 查询细则
     */
    @RequestMapping(value = "/queryDetailed", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryDetailed(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String fid = map.get("fid");
        String schoolType = map.get("school_type");
        String sid = map.get("schoolId");

        if (schoolType == null) {
            schoolType = (String) request.getSession().getAttribute(SessionUser.SCHOOL_TYPE);
            if ("1495071155575000".equals(schoolType)) {
                schoolType = "0";
            } else if ("1495071155575001".equals(schoolType)) {
                schoolType = "1";
            }

        }
        if(sid==null||"".equals(sid)){
           sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
        }

        if (fid == null)
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            School school = this.schoolService.queryById(sid);
            if(school!=null){
                schoolType=school.getP_type_id();
            }

            List<Detailed> list = detailedService.queryDetailedByFid(fid,schoolType);
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "细则不存在!");
            List<Detailed> lt = new ArrayList<>();
            if (null != school) {
                for (int i = 0; i < list.size(); i++) {
                    List<ObservationPoint> list1 = observationPointService.queryObservationPointByFidAndType(list.get(i).getP_id(), school.getP_type_id());
                    for (ObservationPoint map1 : list1) {
                        List<?> list2 = monitorSeeMapRepository.getItemData(map1.getP_id(), sid);
                        map1.setFiles(list2);
                    }
                    Detailed d = new Detailed();
                    d.setP_id(list.get(i).getP_id());
                    d.setP_target_id(list.get(i).getP_target_id());
                    d.setS_content(list.get(i).getS_content());
                    d.setS_create_time(list.get(i).getS_create_time());
                    d.setS_name(list.get(i).getS_name());
                    d.setS_status(list.get(i).getS_status());
                    d.setS_type(list.get(i).getS_type());
                    d.setS_title(list.get(i).getS_title());
                    d.setList(list1);
                    //分项自评表
                    ApplyFile applyFile=applyFileService.queryApplyFileBySchoolId(sid,list.get(i).getP_id());
                    d.setApplyFile(applyFile);
                    //判断细则下测评点是否填写完毕
                    boolean flag = true;
                    for (ObservationPoint o : list1) {
                        if (o.getS_isWrite() == null) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        d.setS_isWrite("1");
                    } else {
                        d.setS_isWrite("0");
                    }
                    lt.add(d);
                }
            } else {









                lt.addAll(list);
            }
//            DetailVo vo=new DetailVo();
//            vo.setDetailList(lt);

//            vo.setApplyFile(applyFile);
            return toResponse(lt, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 查询单个细则
     */
    @RequestMapping(value = "/queryDetailedByPid", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryDetailedByPid(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        if (pid == null)
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            Detailed d = detailedService.queryDetailedByPid(pid);
            if (d == null)
                return toResponse(null, ReturnCode.FAIL, "细则不存在!");
            return toResponse(d, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 添加细则
     */
    @RequestMapping(value = "/saveDetailedInfo", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveDetailedInfo(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String fid = map.get("fid");
        String name = map.get("name");
        String title = map.get("title");
        String content = map.get("content");
        String schoolType = map.get("school_type");
//        if ("0".equals(schoolType)) {
//            schoolType = "1495071155575000";
//        } else if ("1".equals(schoolType)) {
//            schoolType = "1495071155575001";
//        }
        String number = map.get("number");
//		String schoolType = "1";
//		String number = "4";
        if (fid == null || name == null || content == null || StringUtils.isBlank(name) || StringUtils.isBlank(fid) || StringUtils.isBlank(content))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Detailed d = new Detailed();
        d.setP_target_id(fid);
        d.setS_name(name);
        d.setS_create_time(createTime);
        d.setS_content(content);
        d.setS_type("1");
        d.setS_number(number);
        d.setS_school_type(schoolType);
        d.setS_status("1");
        d.setS_title(title);
        detailedService.saveDetailedInfo(d);
        return toResponse(d, ReturnCode.SUCCESS, "添加成功!");
    }

    /**
     * 修改细则详情
     */
    @RequestMapping(value = "/updataDetailedInfo", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updataDetailedInfo(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String name = map.get("name");
        String content = map.get("content");
        String title = map.get("title");
        String pid = map.get("pid");
        if (pid == null || name == null || content == null || StringUtils.isBlank(name) || StringUtils.isBlank(pid) || StringUtils.isBlank(content))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            Detailed d = detailedService.queryDetailedByPid(pid);
            if (d == null)
                return toResponse(null, ReturnCode.FAIL, "细则为空!");
            d.setS_content(content);
            d.setS_name(name);
            d.setS_title(title);
            detailedService.saveDetailedInfo(d);
            return toResponse(d, ReturnCode.SUCCESS, "修改成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "修改失败!");
        }
    }

    /**
     * 删除细则
     */
    @RequestMapping(value = "/deleteDetailedInfo", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteDetailedInfo(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        if (pid == null || StringUtils.isBlank(pid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            Detailed d = detailedService.queryDetailedByPid(pid);
            if (d == null)
                return toResponse(null, ReturnCode.FAIL, "细则为空!");
            d.setS_status("2");
            detailedService.saveDetailedInfo(d);
            return toResponse(d, ReturnCode.SUCCESS, "删除成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除失败!");
        }
    }

    /**
     * 查询所有测评点
     */
    @RequestMapping(value = "/queryAllObservationPoint", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryAllObservationPoint(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String fid = map.get("fid");
        if (fid == null || StringUtils.isBlank(fid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            List<ObservationPoint> list = observationPointService.queryObservationPointByFid(fid);
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "测评点为空!");
            return toResponse(list, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 查询测评点详情
     */
    @RequestMapping(value = "/queryObservationPointByPid", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryObservationPointByPid(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        if (pid == null || StringUtils.isBlank(pid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            ObservationPoint op = observationPointService.queryObservationPointByPid(pid);
            if (op == null)
                return toResponse(null, ReturnCode.FAIL, "测评点为空!");
            return toResponse(op, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 添加测评点
     */
    @RequestMapping(value = "/saveObservationPoint", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveObservationPoint(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String fid = map.get("fid");
        String name = map.get("name");
        String schoolType = map.get("schoolType");
        if ("0".equals(schoolType)) {
            schoolType = "1495071155575000";
        } else {
            schoolType = "1495071155575001";
        }
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        if (fid == null || name == null || schoolType == null || StringUtils.isBlank(name) || StringUtils.isBlank(fid) || StringUtils.isBlank(schoolType))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        ObservationPoint op = new ObservationPoint();
        op.setP_detailed_id(fid);
        op.setS_create_time(createTime);
        op.setS_name(name);
        op.setS_type("1");
        op.setS_status("1");
        op.setS_school_type(schoolType);
        observationPointService.saveObservationPointInfo(op);
        return toResponse(op, ReturnCode.SUCCESS, "添加成功!");
    }

    /**
     * 修改测评点详情
     */
    @RequestMapping(value = "/updataObservationPoint", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updataObservationPoint(HttpServletRequest request, @RequestBody Map<String, String> map) {

        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }
        String name = map.get("name");
        String pid = map.get("pid");
        String schoolType = map.get("schoolType");
        if ("0".equals(schoolType)) {
            schoolType = "1495071155575000";
        } else if ("1".equals(schoolType)) {
            schoolType = "1495071155575001";
        }
        if (pid == null || name == null || schoolType == null || StringUtils.isBlank(name) || StringUtils.isBlank(pid) || StringUtils.isBlank(schoolType))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            ObservationPoint op = observationPointService.queryObservationPointByPid(pid);
            if (op != null)
                op.setS_name(name);
            op.setS_school_type(schoolType);
            observationPointService.saveObservationPointInfo(op);
            return toResponse(op, ReturnCode.SUCCESS, "修改成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "修改失败!");
        }
    }

    /**
     * 删除测评点
     */
    @RequestMapping(value = "/deleteObservationPoint", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteObservationPoint(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        if (pid == null || StringUtils.isBlank(pid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            ObservationPoint op = observationPointService.queryObservationPointByPid(pid);
            if (op != null)
                op.setS_status("2");
            observationPointService.saveObservationPointInfo(op);
            return toResponse(op, ReturnCode.SUCCESS, "删除成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除失败!");
        }
    }

    /**
     * 查询测评点分项
     */
    @RequestMapping(value = "/queryAllMonitorSeeMap", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryAllMonitorSeeMap(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String fid = map.get("fid");
        if (fid == null || StringUtils.isBlank(fid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            List<MonitorSeeMap> list = monitorSeeMapService.queryMonitorSeeMapByFid(fid);
            if (list == null) {
                return toResponse(null, ReturnCode.FAIL, "分项不存在!");
            }
            return toResponse(list, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 查询单个测评点分项
     */
    @RequestMapping(value = "/queryMonitorSeeMapByPid", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryMonitorSeeMapByPid(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        if (pid == null || StringUtils.isBlank(pid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            MonitorSeeMap ms = monitorSeeMapService.queryMonitorSeeMapByPid(pid);
            if (ms == null)
                return toResponse(null, ReturnCode.FAIL, "不存在!");
            return toResponse(ms, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 添加测评点分项
     */
    @RequestMapping(value = "/saveMonitorSeeMap", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveMonitorSeeMap(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String name = map.get("name");
        String desc = map.get("desc");
        String value = map.get("value");
        String type = map.get("type");
        String fid = map.get("fid");
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        if (type == null || StringUtils.isBlank(type) || fid == null || StringUtils.isBlank(fid) || desc == null || StringUtils.isBlank(desc) || value == null || StringUtils.isBlank(value) || name == null || StringUtils.isBlank(name))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        MonitorSeeMap ms = new MonitorSeeMap();
        ms.setP_monitor_id(fid);
        ms.setS_create_time(createTime);
        ms.setS_desc(desc);
        ms.setS_name(name);
        ms.setS_value(value);
        ms.setS_status("1");
        ms.setP_type(type);
        monitorSeeMapService.savaMonitorSeeMapInfo(ms);
        return toResponse(ms, ReturnCode.SUCCESS, "添加成功!");

    }

    /**
     * 修改测评点分项
     */

    @RequestMapping(value = "/updataMonitorSeeMapInfo", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updataMonitorSeeMapInfo(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String name = map.get("name");
        String desc = map.get("desc");
        String value = map.get("value");
        String type = map.get("type");
        String pid = map.get("pid");
        if (type == null || StringUtils.isBlank(type) || pid == null || StringUtils.isBlank(pid) || desc == null || StringUtils.isBlank(desc) || value == null || StringUtils.isBlank(value) || name == null || StringUtils.isBlank(name))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            MonitorSeeMap ms = monitorSeeMapService.queryMonitorSeeMapByPid(pid);
            ms.setS_desc(desc);
            ms.setS_value(value);
            ms.setS_name(name);
            ms.setP_type(type);
            monitorSeeMapService.savaMonitorSeeMapInfo(ms);
            return toResponse(ms, ReturnCode.SUCCESS, "修改成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "修改失败!");
        }
    }

    /**
     * 删除测评点分项
     */
    @RequestMapping(value = "/deleteMonitorSeeMap", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteMonitorSeeMap(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        if (pid == null || StringUtils.isBlank(pid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            MonitorSeeMap ms = monitorSeeMapService.queryMonitorSeeMapByPid(pid);
            ms.setS_status("2");
            monitorSeeMapService.savaMonitorSeeMapInfo(ms);
            return toResponse(ms, ReturnCode.SUCCESS, "删除成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除失败!");
        }
    }

    /*****************************************************学校填报页面**************************************************************************/

    /**
     * 查询该学校的自评表
     */
    @RequestMapping(value = "/queryApplyFileBySchoolId", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryApplyFileBySchoolId(HttpServletRequest request,@RequestBody Map<String, String> map) {
//        String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
//        if (schoolId == null)
//            return toResponse(null, ReturnCode.FAIL, "未登录!");
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String sonTargetId=map.get("son_target_id");
        String schoolId=map.get("schoolId");
        ApplyFileVo vo=new ApplyFileVo();
        try {
            ApplyFile af = applyFileService.queryApplyFileBySchoolId(schoolId,sonTargetId);
            if(af!=null) {
                BeanUtils.copyProperties(af, vo);
            }
            //添加自评表基础信息
            ApplyFileBase base=applyFileService.queryApplyFileBaseBySchoolId(schoolId);
            if(base==null){
                base=new ApplyFileBase();
            }
            vo.setBase(base);
            //添加自评表基础信息详情
            ApplyFileBaseDetail baseDetail=applyFileService.queryApplyFileBaseDetailBySchoolId(schoolId);
            if(baseDetail==null){
                baseDetail=new ApplyFileBaseDetail();
            }
            if(baseDetail.getBuildBase()!=null&&baseDetail.getBuildBase().contains("1")){
                vo.setBuildBase1(1);
            }
            if(baseDetail.getBuildBase()!=null&&baseDetail.getBuildBase().contains("2")){
                vo.setBuildBase2(2);
            }
            vo.setBaseDetail(baseDetail);

            //添加自评表基础信息详情
            ApplyFileBaseScore baseScore=applyFileService.queryApplyFileBaseScoreBySchoolId(schoolId);
            if(baseScore==null){
                baseScore=new ApplyFileBaseScore();
            }
            vo.setBaseScore(baseScore);
            return toResponse(vo, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 保存学校自评表详细信息
     */
    @RequestMapping(value = "/saveApplyFileDetail", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveApplyFileDetail(HttpServletRequest request,@RequestBody ApplyFilePost post) {
        try {
            String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
            if(StringUtils.isEmpty(user_type)){
                return toResponse(null,ReturnCode.FAIL,"暂无权限！");
            }

            ApplyFileBase base=new ApplyFileBase();
            BeanUtils.copyProperties(post,base);
            if(!StringUtils.isBlank(post.getBuildBase1())||!StringUtils.isBlank(post.getBuildBase2())){
            base.setCreateTime(new Date());
            applyFileService.saveAppplyFileBase(base);


            ApplyFileBaseScore baseScore=new ApplyFileBaseScore();
            BeanUtils.copyProperties(post,baseScore);
            baseScore.setCreateTime(new Date());
            applyFileService.saveAppplyFileBaseScore(baseScore);

            ApplyFileBaseDetail baseDetail=new ApplyFileBaseDetail();
            BeanUtils.copyProperties(post,baseDetail);
            baseDetail.setCreateTime(new Date());
            baseDetail.setBuildBase(post.getBuildBase1()+","+post.getBuildBase2());
            applyFileService.saveAppplyFileBaseDetail(baseDetail);
            }else{
                return toResponse(null, ReturnCode.FAIL, "请选择建设基础!");
            }

            return toResponse(null, ReturnCode.SUCCESS, "添加成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(e.getMessage(), ReturnCode.FAIL, "添加失败!");
        }
    }

    /**
     * 保存学校的自评表
     */
    @RequestMapping(value = "/saveApplyFile", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveApplyFile(HttpServletRequest request, @RequestBody Map<String, String> map) {
        //	Map<String ,Object> obj =  (Map<String, Object>) map.get("fileUrl");
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String s_employment_distribution_filepath = map.get("fileUrl");
        String sonTargetId=map.get("son_target_id");
        String schoolId=map.get("schoolId");
        String score=map.get("s_score");
        if (s_employment_distribution_filepath == null) {
            return toResponse(null, ReturnCode.FAIL, "url为空!");
        }
//        schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
//        if (schoolId == null)
//            return toResponse(null, ReturnCode.FAIL, "未登录!");
        Calendar cal = Calendar.getInstance();
        int y = cal.get(Calendar.YEAR);
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String fileName = s_employment_distribution_filepath.substring(s_employment_distribution_filepath.lastIndexOf("/") + 1, s_employment_distribution_filepath.length());
        try {
           ApplyFile af = applyFileService.queryApplyFileBySchoolId(schoolId,sonTargetId);
            if (af == null) {
                ApplyFile a = new ApplyFile();
                a.setS_create_time(createTime);
                a.setS_employment_distribution_filename(fileName);
                a.setS_employment_distribution_filepath(s_employment_distribution_filepath);
                a.setS_school_id(schoolId);
                a.setSon_target_id(sonTargetId);
                a.setS_score(score);
                a.setS_year(y);
                applyFileService.saveAppplyFile(a);
            } else {
                af.setS_create_time(createTime);
                af.setS_employment_distribution_filename(fileName);
                af.setS_employment_distribution_filepath(s_employment_distribution_filepath);
                af.setS_school_id(schoolId);
                af.setSon_target_id(sonTargetId);
                af.setS_year(y);
                af.setS_score(score);
                applyFileService.saveAppplyFile(af);
            }
            return toResponse(af, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }

    }

    /**
     * 删除学校的自评表
     */
    @RequestMapping(value = "/deleteApplyFile", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteApplyFile(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pId=map.get("p_id");
        if (pId == null) {
            return toResponse(null, ReturnCode.FAIL, "删除异常!");
        }
        try {
            applyFileService.deleteApplyFile(pId);
            return toResponse(null, ReturnCode.SUCCESS, "删除成功!");
        } catch (Exception e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除失败!");
        }

    }



    /**
     * 查询该学校填报的数据
     */
    @RequestMapping(value = "/queryObservationPointOrSchoolItemData", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryObservationPointOrSchoolItemData(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
        String fid = map.get("fid");
        if (fid == null || StringUtils.isBlank(fid))
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
		/*String schoolId=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(schoolId ==null)
			return toResponse(null, ReturnCode.FAIL, "未登录!");*/
        try {
            List<MonitorSeeMap> list = monitorSeeMapService.queryMonitorSeeMapPrice(fid, schoolId);
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "查询失败!");
            return toResponse(list, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 保存学校填报的数据
     */
    @RequestMapping(value = "/saveObservationPointOrSchoolItemData", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> saveObservationPointOrSchoolItemData(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
        if (schoolId == null)
            return toResponse(null, ReturnCode.FAIL, "未登录!");
        String item = map.get("itemId");
        String value = map.get("value");
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Calendar cal = Calendar.getInstance();
        int y = cal.get(Calendar.YEAR);

        String[] itemList = item.split("#");
        String[] valueList = value.split("#");
        try {
            School s = schoolService.querySchoolByAccount(schoolId);
            if (s == null)
                return toResponse(null, ReturnCode.FAIL, "该校不存在");
            for (int i = 0; i < valueList.length; i++) {
                SchoolItemData sd = schoolItemDataService.querySchoolItemData(schoolId, itemList[i],y);
                if (sd == null) {
                    SchoolItemData sd1 = new SchoolItemData();
                    sd1.setS_create_time(createTime);
                    sd1.setS_year(y);
                    sd1.setS_item_id(itemList[i]);
                    sd1.setS_value(valueList[i]);
                    sd1.setS_school_id(schoolId);
                    schoolItemDataService.saveSchoolItemData(sd1);
                } else {
                    sd.setS_create_time(createTime);
                    sd.setS_year(y);
                    sd.setS_item_id(itemList[i]);
                    sd.setS_value(sd.getS_value()+","+valueList[i]);
                    sd.setS_school_id(schoolId);
                    schoolItemDataService.updateSchoolItemData(sd);
                }
            }
            return toResponse(null, ReturnCode.SUCCESS, "保存成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "保存失败!");
        }
    }

    /**
     * 提交申请
     */
    @RequestMapping(value = "/updateSchoolState", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> updateSchoolState(HttpServletRequest request) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
        if (schoolId == null)
            return toResponse(null, ReturnCode.FAIL, "未登录!");
        try {
            School s = schoolService.querySchoolByAccount(schoolId);
            if (s == null)
                return toResponse(null, ReturnCode.FAIL, "该校不存在");
            s.setS_state("2");
            schoolService.updateState(s);
            return toResponse(s, ReturnCode.SUCCESS, "YES!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "NO!");
        }
    }

    /**
     * 查询状态
     */
    @RequestMapping(value = "/querySchoolState", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> querySchoolState(HttpServletRequest request) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
        if (schoolId == null)
            return toResponse(null, ReturnCode.FAIL, "未登录!");
        try {
            School s = schoolService.querySchoolByAccount(schoolId);
            if (s == null)
                return toResponse(null, ReturnCode.FAIL, "该校不存在");
            return toResponse(s, ReturnCode.SUCCESS, "YES!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "NO!");
        }
    }

    /**
     * 学校页面--一级指标,二级指标
     */
    @RequestMapping(value = "/queryAllIndexTargetBySchool", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> queryAllIndexTargetBySchool(HttpServletRequest request) {
        try {
            String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
            if(StringUtils.isEmpty(user_type)){
                return toResponse(null,ReturnCode.FAIL,"暂无权限！");
            }

            String sid = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
            String schoolType = (String) request.getSession().getAttribute(SessionUser.SCHOOL_TYPE);
            School school = this.schoolService.queryById(sid);
            List<IndexTargetEntity> list = indexTargetEntityService.queryAllIndexTarget();
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "暂无数据");
            List<IndexTargetEntity> lt = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                List<SonTarget> list1 = sonTargetService.querySonTargetByFid(list.get(i).getP_id());
                IndexTargetEntity it = new IndexTargetEntity();
                it.setP_id(list.get(i).getP_id());
                it.setS_name(list.get(i).getS_name());
                it.setS_create_time(list.get(i).getS_create_time());
                it.setS_status(list.get(i).getS_status());
                it.setS_type(list.get(i).getS_type());
                //判断二级指标以下是否填写完毕
                for (int k = 0; k < list1.size(); k++) {
                    List<Detailed> details = this.detailedService.queryDetailedByFid(list1.get(k).getP_id(), schoolType);
                    for (int j = 0; j < details.size(); j++) {
                        List<ObservationPoint> points = this.observationPointService.queryObservationPointByFidAndType(details.get(j).getP_id(), school.getP_type_id());
                        boolean flag = true;
                        for (ObservationPoint o : points) {
                            if (o.getS_isWrite() == null) {
                                flag = false;
                            }
                        }
                        if (flag)
                            details.get(j).setS_isWrite("1");
                    }
                    boolean flag1 = true;
                    for (Detailed d : details) {
                        if (d.getS_isWrite() == null) {
                            flag1 = false;
                        }
                    }
                    if (flag1)
                        list1.get(k).setS_isWrite("1");
                }
                //判断一级指标是否填写完毕
                boolean flag2 = true;
                for (SonTarget s : list1) {
                    if (s.getS_isWrite() == null) {
                        flag2 = false;
                    }
                }
                if (flag2) {
                    it.setS_isWrite("1");
                }
                it.setList(list1);
                lt.add(it);
            }
            return toResponse(lt, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 查询细则
     */
    @RequestMapping(value = "/queryDetailedAndObservationPoint", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryDetailedAndObservationPoint(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String fid = map.get("fid");
//		String schoolType = map.get("school_type");
        String schoolType = map.get("0");
        if (fid == null)
            return toResponse(null, ReturnCode.FAIL, "参数为空!");
        try {
            List<Detailed> list = detailedService.queryDetailedByFid(fid, schoolType);
            if (list == null)
                return toResponse(null, ReturnCode.FAIL, "细则不存在!");
            List<Detailed> lt = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                List<ObservationPoint> list1 = observationPointService.queryObservationPointByFid(fid);
                Detailed d = new Detailed();
                d.setP_id(list.get(i).getP_id());
                d.setP_target_id(list.get(i).getP_target_id());
                d.setS_content(list.get(i).getS_content());
                d.setS_create_time(list.get(i).getS_create_time());
                d.setS_name(list.get(i).getS_name());
                d.setS_status(list.get(i).getS_status());
                d.setS_type(list.get(i).getS_type());
                d.setList(list1);
                lt.add(d);
            }
            return toResponse(lt, ReturnCode.SUCCESS, "查询成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败!");
        }
    }

    /**
     * 重置学校提交状态
     */
    @RequestMapping(value = "/resetSchoolSubmit", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> resetSchoolSubmit(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
        if (pId == null)
            return toResponse(null, ReturnCode.FAIL, "未登录!");
        String schoolId = map.get("schoolId");
        try {
            schoolService.resetSchoolStateBySchool(schoolId);
            return toResponse(null, ReturnCode.SUCCESS, "重置提交成功!");
        } catch (SQLException e) {
            log.error(e);
            return toResponse(null, ReturnCode.FAIL, "重置提交失败!");
        }
    }

}

